package com.eastedu.report.common.util;

import java.text.NumberFormat;
import java.util.Locale;
import java.util.Objects;

/**
 * 浮点数处理
 *
 * @author wanqt
 */
public interface NumberFormatUtil {


    /**
     * Format percent string.
     *
     * @param num the num
     * @return the string
     */
    static String formatPercent(Number num) {
        return formatPercent(num, "");
    }

    /**
     * Format percent string.
     *
     * @param num        the num
     * @param defaultVal the default val
     * @return the string
     */
    static String formatPercent(Number num, String defaultVal) {
        if (Objects.isNull(num)) {
            return defaultVal;
        }
        NumberFormat percentFormat = NumberFormat.getPercentInstance(Locale.getDefault());
        percentFormat.setMinimumFractionDigits(1);
        percentFormat.setMaximumFractionDigits(1);
        return percentFormat.format(num);
    }
}
